namespace ShoppingCart.WebSite.Admin
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Business.Logic;
    using Code;
    using DTO = Business.Entity;

    public partial class Bancos : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            CargarBancos();
        }

        protected void GvBancosPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBancos.PageIndex = e.NewPageIndex;
            CargarBancos();
        }

        protected void GvBancosRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Editar":
                    Response.Redirect(string.Format("Banco.aspx?idBanco={0}&Accion=M", Convert.ToInt32(e.CommandArgument)), true);
                    break;
                case "Eliminar":
                    EliminarBanco(Convert.ToInt32(e.CommandArgument));
                    break;
            }
        }

        private void CargarBancos()
        {
            var entidades = BancoManager.Instancia.Listar();
            BindControl<DTO.Banco>.BindGrid(gvBancos, entidades);
            litMensaje.Text = string.Empty;
        }

        private void EliminarBanco(int idBanco)
        {
            try
            {
                BancoManager.Instancia.Eliminar(idBanco);
                CargarBancos();
                litMensaje.Text = GetMessage("Banco fue eliminado satisfactoriamente.", MessageType.success);
            }
            catch (Exception ex)
            {
                litMensaje.Text = GetMessage(ex.Message, MessageType.errormsg);
            }
        }
    }
}
